// package-star/pages/warehouse-setting/index.js
import { ManageTargetInfoUpdateMixins, PageScrollListMixins } from '../../../behaviors/page';
import { PACKAGE_STAR_PAGE } from '../../../constants/route';
import { navTo, showLoading, hideLoading } from '../../../common/env';
import { getWarehouseList, removeWarehouse } from '../../../api/brd';
let loaded = false;

Page({
    mixins: [ManageTargetInfoUpdateMixins, PageScrollListMixins],
    data: {
    },
    onLoad(options) {
    },
    async _handleGetList(pageNo) {
        !this.data.loaded && showLoading('加载中...');
        const { pageData } = this.data;
        let { ts } = pageData;
        pageNo === 1 && (ts = Date.now());
        const { rows = [], total } = await getWarehouseList({ pageNo, ts });
        this.setData({
            pageData: {
                ...pageData,
                refresh: false,
                ts,
                pageNo,
                total,
                rows: pageNo == 1 ? rows : [...pageData.rows, ...rows],
            },
        });
        if (!this.data.loaded) hideLoading(); this.setData({ loaded: true });
    },
    handleRemove(e) {
        const _ = this;
        const warehouseId = e.detail;
        TipDialog({
            title: '确定删除仓库？',
            ok() {
                removeWarehouse(warehouseId).then(flag => {
                    if (flag) _._handleGetList(1);
                });
            },
        });
    },
    handleClick(e) {
        const warehouseId = e.detail;
        navTo(PACKAGE_STAR_PAGE['brandWarehouseEdit'], {
            warehouseId,
        });
    },
    handleCreate() {
        navTo(PACKAGE_STAR_PAGE['brandWarehouseEdit']);
    },
})
